package com.swlz.service.impl;

import java.sql.SQLException;

import com.swlz.dao.DepartmentDAO;
import com.swlz.entity.Department;
import com.swlz.exception.CodeException;
import com.swlz.service.DepartmentService;
import com.swlz.vo.PageVO;

/**
 * <pre>
 * 业务逻辑接口具体实现类
 * 系统部门
 * ClassName : com.swlz.service.impl.DepartmentServiceImpl
 * Author :swlz
 * E-Mail : 2233875735@qq.com
 * Date : 2020-06-26 17:00:53
 * Version : 1.0
 * Copyright 2020 swlz.top Inc. All rights reserved. 
 * Warning: this content is only for internal circulation of the company. It is forbidden to divulge it or use it for other commercial purposes
 * </pre>
 **/
public class DepartmentServiceImpl implements DepartmentService {
	
	private DepartmentDAO departmentDAO = new DepartmentDAO();

	@Override
	public int add(Department department) throws CodeException {
		try {
			return departmentDAO.insertSelective(department);
		} catch (SQLException e) {
			throw new CodeException(500, "添加系统部门错误",e);
		}
	}

	@Override
	public int update(Department department) throws CodeException {
		try {
			return departmentDAO.updateSelective(department);
		} catch (SQLException e) {
			throw new CodeException(501, "修改系统部门错误",e);
		}
	}

	@Override
	public int delete(Long deptId) throws CodeException {
		try {
			return departmentDAO.deleteById(deptId);
		} catch (SQLException e) {
			throw new CodeException(502, "删除系统部门错误",e);
		}
	}

	@Override
	public Department get(Long deptId) throws CodeException {
		try {
			return departmentDAO.selectById(deptId);
		} catch (SQLException e) {
			throw new CodeException(503, "获取系统部门失败",e);
		}
	}

	@Override
	public PageVO<Department> page(Department department, Integer current, Integer rowMax) throws CodeException {
		try {
			long total = departmentDAO.countByModel(department);
			PageVO<Department> page = new PageVO<>(total, current, rowMax);
			return page.setList(departmentDAO.selectByModel(department, page.getOffset(), page.getRowMax()));
		} catch (SQLException e) {
			throw new CodeException(504, "获取系统部门分页数据失败",e);
		}
	}


}
